import httpFetch from "share/httpFetch";
import config from "config";
import { formatNumber, messages } from "share/common";
import { commonPieReportFactory, commonBarReportFactory, costCenterReportFactory, feeTendencyReportFactory } from "../echarts-data-transfer";
import { lengthStrShow } from "../../report-utils";

export default {
  // 获取费用分析报表明细-总金额，总人数，平均金额
  getReportDetailTotal(params) {
    return new Promise((resolve, reject) => {
      httpFetch.post(config.baseUrl + '/report/api/search/expense/analysis/report/detail/total', params)
        .then((response) => {
          let { baseAmount, employeeQTY, avgBaseAmount } = response.data || {};
          let dataLength = 1;
          let options = [{
            title: messages('finance-7.key194')/*总报销金额*/,
            detail: baseAmount ? formatNumber(baseAmount) : 0
          }, {
            title: messages('finance-7.key195')/*总人数*/,
            detail: employeeQTY
          }, {
            title: messages('finance-7.key196')/*员工平均金额*/,
            detail: avgBaseAmount ? formatNumber(avgBaseAmount) : 0
          }];
          resolve({
            dataLength,
            options
          });
        })
    })
  },

  // 费用总体趋势报表查询接口：
  getExpenseAnalysisReport(params) {
    return new Promise((resolve, reject) => {
      httpFetch.post(config.baseUrl + '/report/api/search/expense/analysis/report/detail/expense/total', params)
        .then((response) => {
          let data = response.data || [];
          let dataLength = data.length;
          let options;
          // params.filterShowYOY  默认没有值也是有同比的
          if (params.filterShowYOY === true || params.filterShowYOY === undefined) {
            options = feeTendencyReportFactory(data, true)
          } else {
            options = feeTendencyReportFactory(data)
          }
          resolve({
            dataLength,
            options
          });
        })
    })
  },

  // 获取费用分析报表明细-费用类型图表
  getReportDetailExpenseType(params) {
    return new Promise((resolve, reject) => {
      httpFetch.post(config.baseUrl + '/report/api/search/expense/analysis/report/detail/expense/type', params)
        .then((response) => {
          let res = {};
          let data = response.data || [];
          res.dataLength = data.length;
          res.options = commonBarReportFactory(data, 0, undefined, true);
          res.options.series[0].name = messages('finance-7.key310')/*金额(元)*/;
          res.options.legend.data = [messages('finance-7.key310')/*金额(元)*/];
          resolve(res);
        })
    })
  },

  // 获取费用分析报表明细-部门费用图表
  getReportDetailDepartmentExpense(params) {
    // {
    //   "reportId":10010
    //   // 还有的筛选器条件
    // }
    return new Promise((resolve, reject) => {
      httpFetch.post(config.baseUrl + '/report/api/search/expense/analysis/report/detail/department/expense', params)
        .then((response) => {
          let res = {};
          let data = response.data || [];
          res.dataLength = data.length;
          res.options = commonBarReportFactory(data, 1, undefined, true);
          res.options.series[0].name = messages('finance-7.key310')/*金额(元)*/;
          res.options.legend.data = [messages('finance-7.key310')/*金额(元)*/];
          res.options.tooltip.formatter = (params = []) => {
            let item = params[0] || {};
            item.data = item.data || {};
            return `${lengthStrShow(item.data.departmentPath)}:<br>${formatNumber(item.value)}<br>${item.data._percentageStr}`
          };
          resolve(res);
        })
    })
  },

  // 获取费用分析报表明细-报销状态图表
  getReportDetailReportStatus(params) {
    // {
    //   "reportId":10010
    //   // 还有的筛选器条件
    // }
    return new Promise((resolve, reject) => {
      httpFetch.post(config.baseUrl + '/report/api/search/expense/analysis/report/detail/report/status', params)
        .then((response) => {
          let res = {};
          let data = response.data || [];
          res.dataLength = data.length;
          res.options = commonPieReportFactory(data, true);
          resolve(res);
        })
    })
  },

  //人员费用占比图表接口
  getReportDetailUserExpense(params) {
    return new Promise((resolve, reject) => {
      httpFetch.post(config.baseUrl + '/report/api/search/expense/analysis/report/detail/user/expense', params)
        .then((response) => {
          let res = {};
          let data = response.data || [];
          res.dataLength = data.length;
          res.options = commonBarReportFactory(data, 2, undefined, true);
          res.options.series[0].name = messages('finance-7.key310')/*金额(元)*/;
          res.options.legend.data = [messages('finance-7.key310')/*金额(元)*/];
          resolve(res);
        })
    })
  },

  // 人员费用明细接口：
  getReportDetailUserDetailExpense(params, pagination) {
    // {
    //   "reportId":10010
    //   // 还有的筛选器条件
    // }
    return new Promise((resolve, reject) => {
      httpFetch.post(config.baseUrl + '/report/api/search/expense/analysis/report/detail/user/expense/detail/list' +
        '?page=' + pagination.page + "&size=" + pagination.size, params)
        .then((response) => {
          resolve(response);
        })
    })
  },



  //获取成本中心-成本中心报表
  getReportDetailCostCenter(params) {
    // {
    //   "reportId":10010
    //   // 还有的筛选器条件
    // }
    return new Promise((resolve, reject) => {
      httpFetch.post(config.baseUrl + '/report/api/search/expense/analysis/report/detail/cost/center/expense', params)
        .then((response) => {
          let res = {};
          let data = response.data || [];
          res.dataLength = data.length;
          res.options = costCenterReportFactory(data, 3);
          resolve(res);
        })
    })
  },
}
